﻿using InteControlSys.SampleService;
using InteControlSys.ViewModel;
using Microsoft.Win32;
using PropertyTools.Wpf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Linq;

namespace InteControlSys.ViewModel
{
    public class SampleRecordsViewModel:NodeViewModel
    {
         public override Uri GetMyIcon()
        {
            return new Uri("../Resources/Images/bucket.png", UriKind.RelativeOrAbsolute);
        }

         public SampleRecordsViewModel(string name)
        {
            this.Name = name;
            this.Parent = null;
            this.IsExpanded = true;
            this.NodeType = "SampleRecordsViewModel";
            this.OperatingTruckList = new ObservableCollection<NodeViewModel>();
            dpBegin = DateTime.Now.AddDays(-7);
            dpEnd = DateTime.Now;
            Init();
        }

         public SampleRecordsViewModel()
         {
             dpBegin = DateTime.Now.AddDays(-7);
             dpEnd = DateTime.Now;
             Init();
         }

         public SampleService.SampleClient sp = new SampleClient("WSHttpBinding_ISample");
         public SampleCommon sc = new SampleCommon();

         public void Init()
         {
             var bs = sp.GetBucketWithDate(dpBegin, dpEnd).OrderBy(o => o.AnalysGroup.质检凭证号);
             sampleRecords = new ObservableCollection<SampleRecords>();
             foreach (var item in bs)
             {
                 var sr = new SampleRecords();
                 sr.AnalysBatchID = item.AnalysGroup.质检凭证号;
                 sr.BucketID = item.样桶号;
                 sr.BucketStatus = sc.DicBucketStatus[item.大样状态];
                 sr.Deliver = item.送样人 != null ? item.送样人.CName : string.Empty;
                 sr.DeliveTime = item.送样时间;
                 sr.Materiel = item.AnalysGroup.物料.Name;
                 sr.Weight = item.大样重量;
                 sr.SampleMachine = sc.DicSampleMachine[item.取样设备号];
                 sr.SealedTime = item.大样封装时间;
                 var sm = sp.GetMakeByGID(item.AnalysGroup.ID);
                 if (null != sm)
                 {
                     sr.SampleMaker = sm.SampleMaker;
                     sr.SampleMakeTime = sm.SampleMakeTime;
                     sr.SampleRecieveTime = sm.SampleRecieveTime;
                 }
                 sampleRecords.Add(sr);
             }
             this.SampleRecords = sampleRecords;
         }

         public ICommand Query
         {
             get
             {
                 return new DelegateCommand(() =>
                 {
                     Init();
                 });
             }
         }

        #region properties
         private DateTime _dpBegin;

         public DateTime dpBegin
         {
             get { return _dpBegin; }
             set
             {
                 _dpBegin = value;
                 NotifyChanged("dpBegin");
             }
         }

         private DateTime _dpEnd;

         public DateTime dpEnd
         {
             get { return _dpEnd; }
             set
             {
                 _dpEnd = value;
                 NotifyChanged("dpEnd");
             }
         }

         private ObservableCollection<SampleRecords> sampleRecords;
         public ObservableCollection<SampleRecords> SampleRecords
         {
             get { return sampleRecords; }
             set
             {
                 sampleRecords = value;
                 NotifyChanged("SampleRecords");
             }
         }


        #endregion
    }

    public class SampleRecords
    {
        public SampleRecords() { }

        public string AnalysBatchID { get; set; }
        public string BucketID { get; set; }
        public string BucketStatus { get; set; }
        public string Materiel { get; set; }
        public double? Weight { get; set; }

        public string SampleMachine { get; set; }
        public DateTime? SealedTime { get; set; }

        public DateTime? DeliveTime { get; set; }
        public string Deliver { get; set; }

        public DateTime? SampleRecieveTime { get; set; }

        public string SampleMaker { get; set; }
        public DateTime? SampleMakeTime { get; set; }


    }
}
